package com.example.springbott.wx_service;

import com.example.springbott.bean.*;
import com.example.springbott.bean.silic.WXAftersaleDetailsVo;
import com.example.springbott.mapper.MarketAftersaleMapper;
import com.example.springbott.mapper.MarketOrderGoodsMapper;
import com.example.springbott.mapper.MarketOrderMapper;
import com.example.springbott.util.Count;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 售后模块
 * @author silic xia
 * @since 2022/11/23 15:19
 */
@Service
public class WXAftersaleServiceImpl implements WXAftersaleService{

    @Autowired
    MarketAftersaleMapper aftersaleMapper;
    @Autowired
    MarketOrderMapper orderMapper;
    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;

    /**
     *  申请售后 details——展示功能
     *
     * @param /orderId=153
     * @return BaseRespVo--aftersale/order/orderGoods
     * @author  silic xia
     * @since 2022/11/23 15:12
     */
    @Override
    public WXAftersaleDetailsVo wxAftersaleDetails(String orderId) {

        MarketAftersaleExample example = new MarketAftersaleExample();
        MarketAftersaleExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(Integer.valueOf(orderId));
        List<MarketAftersale> marketAftersales = aftersaleMapper.selectByExample(example);

        MarketOrderExample example1 = new MarketOrderExample();
        MarketOrderExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andIdEqualTo(Integer.valueOf(orderId));
        List<MarketOrder> marketOrders = orderMapper.selectByExample(example1);

        MarketOrderGoodsExample example2 = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andOrderIdEqualTo(Integer.valueOf(orderId));
        List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(example2);

        WXAftersaleDetailsVo wxAftersaleDetailsVo = new WXAftersaleDetailsVo();
        wxAftersaleDetailsVo.setAftersale(marketAftersales.get(0));
        wxAftersaleDetailsVo.setOrder(marketOrders.get(0));
        wxAftersaleDetailsVo.setOrderGoods(marketOrderGoods);

        return wxAftersaleDetailsVo;
    }

    /**
     * 申请售后 submit——提交
     *
     * @param {pictures: [], orderId: "160", amount: 206, type: 0, typeDesc: "未收货退款", reason: "111"}
     * {"pictures":[""],"orderId":"161","amount":206,"type":1,"typeDesc":"不退货退款","reason":"111"}
     * {pictures: [], orderId: "158", amount: 206, type: 2, typeDesc: "退货退款", reason: "111"}
     *
     * @return {errno: 0, errmsg: "成功"}
     * @author silic xia
     * @since 2022/11/23 15:53
     */
    @Override
    public void wxAftersaleSubmit(MarketAftersale marketAftersale) {

        // 修改订单状态
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setOrderStatus(Count.ORDER_STATUS_REFUNDABLE);
        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketAftersale.getOrderId());
        orderMapper.updateByExampleSelective(marketOrder,example);

        // 插入数据
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String date = format.format(new Date());
        String aftersaleSn = date + marketAftersale.getOrderId() + user.getId();
        marketAftersale.setAftersaleSn(aftersaleSn);
        marketAftersale.setUserId(user.getId());
        marketAftersale.setAddTime(new Date());
        marketAftersale.setUpdateTime(new Date());

        aftersaleMapper.insertSelective(marketAftersale);

    }
}

